﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
using Tool;

namespace AutoMachine
{
    public partial class Frm_Product : Form
    {
        Tool.AutoResize FormResize = new Tool.AutoResize();//窗体缩放实例化    

        #region 窗体实例化
        /// <summary>
        /// 窗体对象实例化
        /// </summary>
        private static Frm_Product _instance;
        internal static Frm_Product Instance
        {
            get
            {
                if (_instance == null)
                    _instance = new Frm_Product();
                return _instance;
            }
        }
        #endregion
           
        public Frm_Product()
        {
            InitializeComponent();

            FormResize.SetAutoResize(this);//窗体缩放实例化   
       
        }


        string[] ComString = new string[] { "柱状图", "饼形图", "折线图", "横条图", "圆环图", "样条图", "样条面积图"};

        private void Frm_Product_Load(object sender, EventArgs e)
        {
            for (int i = 0; i < Count.DayTime.Length; i++)
            {
                int data = Convert.ToInt16(Count.DayTime[i]);
                dgvCounter.Rows.Add(new object[] { data + "时", Convert.ToString(Count.OKArray[data]), Convert.ToString(Tool.Count.NGArray[data]), Convert.ToString(Tool.Count.TotalArray[data]) });
            }

            for (int i = 0; i < Count.NightTime.Length; i++)
            {
                int data1 = Convert.ToInt16(Count.NightTime[i]);
                dgvCounter1.Rows.Add(new object[] { data1 + "时", Convert.ToString(Tool.Count.OKArray[data1]), Convert.ToString(Tool.Count.NGArray[data1]), Convert.ToString(Tool.Count.TotalArray[data1]) });
            }

            Tool.Count.CountRefreshedevent += Count_CountRefreshedevent;

            this.comboBox1.Items.Clear();

            this.comboBox2.Items.Clear();

            //ComBox添加数据
            for (int i = 0; i < ComString.Length; i++)
            {
                this.comboBox1.Items.Add(ComString[i]);
            }

            this.comboBox1.SelectedIndex = 1;//索引项

            ChartShow.ShowChart(this.chart1, SeriesChartType.Column, Count.DayTime);
            ChartShow.ShowChart(this.chart2, SeriesChartType.Column, Count.NightTime);

            string filename = SysConfig.Config["历史数据路径"].ToString();

            //遍历文件夹下的CSV文件

            ////遍历文件夹下的CSV文件
            //DirectoryInfo fdir = new DirectoryInfo(filename);

            //FileInfo[] files = fdir.GetFiles("*.csv");

            //foreach (FileInfo d in files)//遍历文件夹
            //{
            //    comboBox2.Items.Add(d.Name);//添加到ComBox中
            //}

        }

        private void Count_CountRefreshedevent()
        {
            this.Invoke(new Action(() =>
            {
                dgvCounter.Rows.Clear();
                dgvCounter1.Rows.Clear();

                for (int i = 0; i < Count.DayTime.Length; i++)
                {
                    int data = Convert.ToInt16(Count.DayTime[i]);
                    dgvCounter.Rows.Add(new object[] { data + "时", Convert.ToString(Count.OKArray[data]), Convert.ToString(Tool.Count.NGArray[data]), Convert.ToString(Tool.Count.TotalArray[data]) });
                }

                for (int i = 0; i < Count.NightTime.Length; i++)
                {
                    int data1 = Convert.ToInt16(Count.NightTime[i]);
                    dgvCounter1.Rows.Add(new object[] { data1 + "时", Convert.ToString(Tool.Count.OKArray[data1]), Convert.ToString(Tool.Count.NGArray[data1]), Convert.ToString(Tool.Count.TotalArray[data1]) });
                }
            }));
        }

        private void 刷新数据ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Tool.Count.CountClear();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string name = (string)comboBox1.SelectedItem;
            if (name == "柱状图")
            {
                ChartShow.ShowChart(this.chart1, SeriesChartType.Column, Count.DayTime);
                ChartShow.ShowChart(this.chart2, SeriesChartType.Column, Count.NightTime);

            }
            else if (name == "饼形图")
            {
                ChartShow.ShowChart(this.chart1, SeriesChartType.Pie, Count.DayTime);
                ChartShow.ShowChart(this.chart2, SeriesChartType.Pie, Count.NightTime);
            }
            else if (name == "折线图")
            {
                ChartShow.ShowChart(this.chart1, SeriesChartType.Line, Count.DayTime);
                ChartShow.ShowChart(this.chart2, SeriesChartType.Line, Count.NightTime);
            }
            else if (name == "横条图")
            {
                ChartShow.ShowChart(this.chart2, SeriesChartType.Bar, Count.DayTime);
                ChartShow.ShowChart(this.chart3, SeriesChartType.Bar, Count.NightTime);
            }
            else if (name == "圆环图")
            {
                ChartShow.ShowChart(this.chart1, SeriesChartType.Doughnut, Count.DayTime);
                ChartShow.ShowChart(this.chart2, SeriesChartType.Doughnut, Count.NightTime);

            }
            else if (name == "样条图")
            {
                ChartShow.ShowChart(this.chart1, SeriesChartType.Spline, Count.DayTime);
                ChartShow.ShowChart(this.chart2, SeriesChartType.Spline, Count.NightTime);
            }
            else if (name == "样条面积图")
            {
                ChartShow.ShowChart(this.chart1, SeriesChartType.SplineArea, Count.DayTime);
                ChartShow.ShowChart(this.chart2, SeriesChartType.SplineArea, Count.NightTime);
            }
        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            string name = (string)comboBox2.SelectedItem;

            string filename = SysConfig.Config["历史数据路径"].ToString() + "\\" + name;

            WritePointSystem(filename);

            ChartShow.ShowChart1(this.chart3, SeriesChartType.Column, Count.DayTime);

        }




        #region 初始化加载CSV文件数据 并写入到List列表中

        public static void WritePointSystem(String Path)
        {
          

            GlobalVar.Datas.Clear();//数据清零
            ReadCsv(Path, out GlobalVar.Datas);

            GlobalVar.ListTotal.Clear();//清空集合
            GlobalVar.ListOK.Clear();
            GlobalVar.ListNG.Clear();
            for (int i = 1; i < GlobalVar.Datas.Count; i++)
            {
                //从第四行开始读取 0 0 0 0
                GlobalVar.ListTotal.Add(GlobalVar.Datas[i].Split(',')[1]);//第一个1 是行 第二个1是列 X轴
                GlobalVar.ListOK.Add(GlobalVar.Datas[i].Split(',')[2]);//第一个1 是行 第二个2是列 Y轴
                GlobalVar.ListNG.Add(GlobalVar.Datas[i].Split(',')[3]);//第一个1 是行 第二个3是列 Z轴
                   
            }
        }

        //读取csv
        public static void ReadCsv(string path, out List<string> data)
        {
            StreamReader sr;
            data = new List<string>();
            try
            {
                using (sr = new StreamReader(path, Encoding.GetEncoding("GB2312")))
                {
                    string str = "";
                    while ((str = sr.ReadLine()) != null)
                    {
                        data.Add(str);
                    }
                }
            }
            catch (Exception ex)
            {
                foreach (Process process in Process.GetProcesses())
                {
                    if (process.ProcessName.ToUpper().Equals("EXCEL"))
                        process.Kill();
                }
                GC.Collect();
                Thread.Sleep(10);
                Console.WriteLine(ex.StackTrace);
                using (sr = new StreamReader(path, Encoding.GetEncoding("GB2312")))
                {
                    string str = "";
                    while ((str = sr.ReadLine()) != null)
                    {
                        data.Add(str);
                    }
                }
            }

        }




        #endregion

       
    }
}
